﻿Imports System.ComponentModel

'TODO: Melhorar esse controle. Ainda não foi usado e nem testado.

Namespace Controle

    <Themeable(True), ToolboxData("<{0}:YCBox runat=""server""></{0}:YCBox>")> _
    Public Class YCBox
        Inherits WebControl
        Implements INamingContainer

#Region "Variavel"

        Private pHeaderControl As Control
        Private pHeader As ITemplate

        Private pBodyControl As Control
        Private pBody As ITemplate

        Private pFootControl As Control
        Private pFoot As ITemplate

        Private pHfStatus As HiddenField 'HtmlControls.HtmlInputHidden
        Private pStatus As Boolean

#End Region

#Region "Propriedades"

        <PersistenceMode(PersistenceMode.InnerProperty), Browsable(False), TemplateInstance(TemplateInstance.Single)> _
        Public Property Header() As ITemplate
            Get
                Return pHeader
            End Get
            Set(ByVal value As ITemplate)
                pHeader = value
            End Set
        End Property

        <PersistenceMode(PersistenceMode.InnerProperty), Browsable(False), TemplateInstance(TemplateInstance.Single)> _
        Public Property Body() As ITemplate
            Get
                Return pBody
            End Get
            Set(ByVal value As ITemplate)
                pBody = value
            End Set
        End Property

        <PersistenceMode(PersistenceMode.InnerProperty), Browsable(False), TemplateInstance(TemplateInstance.Single)> _
        Public Property Foot() As ITemplate
            Get
                Return pFoot
            End Get
            Set(ByVal value As ITemplate)
                pFoot = value
            End Set
        End Property

        Public Property Status() As Boolean
            Get
                Return CBool(pHfStatus.Value)
            End Get
            Set(ByVal value As Boolean)
                pStatus = value
            End Set
        End Property

#End Region

#Region "Métodos"

        Private Sub CreateContents()
            Me.Controls.Clear()
            MyBase.CreateChildControls()

            pHeaderControl = New Control
            Me.Controls.Add(pHeaderControl)

            pBodyControl = New Control
            Me.Controls.Add(pBodyControl)

            pFootControl = New Control
            Me.Controls.Add(pFootControl)

            pHfStatus = New HiddenField
            Me.Controls.Add(pHfStatus)

            '------------------------------------------------------

            If pHeader IsNot Nothing Then
                pHeader.InstantiateIn(pHeaderControl)
            End If

            If pBody IsNot Nothing Then
                pBody.InstantiateIn(pBodyControl)
            End If

            If pFoot IsNot Nothing Then
                pFoot.InstantiateIn(pFootControl)
            End If

        End Sub

#End Region

        Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
            MyBase.OnInit(e)
            CreateContents()
            MyBase.EnsureChildControls()
        End Sub

        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            'MyBase.Render(writer)
            With writer
                MyBase.RenderBeginTag(writer)
                'If pStatus Then
                '    .Write("ativado")
                'Else
                '    .Write("desativado")
                'End If

                .Write("<table>")
                .Write("<thead>")
                .Write("<tr>")
                .Write("<td>")
                pHeaderControl.RenderControl(writer)
                .Write("</td>")
                .Write("</tr>")
                .Write("</thead>")
                .Write("<tbody>")
                .Write("<tr>")
                .Write("<td>")
                pBodyControl.RenderControl(writer)
                .Write("</td>")
                .Write("</tr>")
                .Write("</tbody>")
                .Write("<tfoot>")
                .Write("<tr>")
                .Write("<td>")
                pFootControl.RenderControl(writer)
                .Write("</td>")
                .Write("</tr>")
                .Write("</tfoot>")
                .Write("</table>")
                pHfStatus.RenderControl(writer)
                MyBase.RenderEndTag(writer)
            End With
        End Sub

    End Class

End Namespace